package com.shhic.hhplt.server.biz;

import java.io.File;
import java.io.IOException;
import java.util.*;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.shhic.hhplt.server.model.BindingCodeQuery;
import com.shhic.hhplt.server.model.BindingCodeQueryResult;

@Component
@SuppressWarnings("all")
public class ExportReportHelper implements IExportReportHelper{
	@Value("${report.location}")
	private String reportLocation;

	private String getBindingQueryFileName(BindingCodeQuery query){
		String ser = Long.toString(System.currentTimeMillis());
		return query.getStartCode()+"~"+query.getEndCode() + "_"+ser+".xls";
	}
	@Override
	public String exportBindingCodeToExcelFile(
			List<BindingCodeQueryResult> resultList, BindingCodeQuery query) {
		String filename = getBindingQueryFileName(query);
		File dir=new File(reportLocation);
		if(!dir.exists()){
			dir.mkdir();
		}
		File file = new File(reportLocation+ filename);
		try {
			WritableWorkbook workbook = Workbook.createWorkbook(file);
			WritableSheet sheet = workbook.createSheet("First Sheet",0);
			
			sheet.addCell(new Label(0,0,query.getConditionCodeName()));
			sheet.addCell(new Label(1,0,"产品标识码"));

			int colsum = 0;
			for(String bn:query.getBindingCodeNameList()){
				sheet.addCell(new Label((colsum++) + 2,0,bn));
			}

			int row = 1;
			for(BindingCodeQueryResult r:resultList){
				sheet.addCell(new Label(0,row,r.getQueryCode()));
				sheet.addCell(new Label(1,row,r.getIdCode()));
				switch(colsum){
				case 9:
					sheet.addCell(new Label(10,row,r.getCode_8()));
				case 8:
					sheet.addCell(new Label(9,row,r.getCode_7()));
				case 7:
					sheet.addCell(new Label(8,row,r.getCode_6()));				
				case 6:
					sheet.addCell(new Label(7,row,r.getCode_5()));
				case 5:
					sheet.addCell(new Label(6,row,r.getCode_4()));
				case 4:
					sheet.addCell(new Label(5,row,r.getCode_3()));
				case 3:
					sheet.addCell(new Label(4,row,r.getCode_2()));
				case 2:
					sheet.addCell(new Label(3,row,r.getCode_1()));
				case 1:
					sheet.addCell(new Label(2,row,r.getCode_0()));
				}
				row++;
			}
			workbook.write();
			workbook.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return filename;
	}

	private String getBindingQueryFileName(String value){
		String ser = Long.toString(System.currentTimeMillis());
		return value+".xls";
	}
	@Override
	public List<String> exportBindingCodeToExcelFileOfMany(List<BindingCodeQueryResult> resultList, BindingCodeQuery query) {
		Set<String> set=new HashSet<>();
		List<BindingCodeQueryResult> resultCopy=new ArrayList<>();
		for (BindingCodeQueryResult result : resultList) {
			set.add(result.getQueryCode());
		}
		List<String> filenameList=new ArrayList<>();
		int execelTotal=set.size();
		Iterator<String> iterator=set.iterator();
		while(iterator.hasNext()){
			String caseCode=iterator.next();
			String filename = getBindingQueryFileName(caseCode);
			File file = new File(reportLocation+ filename);
			for (BindingCodeQueryResult result : resultList) {
				if(result.getQueryCode().equals(caseCode)){
					resultCopy.add(result);
				}
			}
			try {
				WritableWorkbook workbook = Workbook.createWorkbook(file);
				WritableSheet sheet = workbook.createSheet("First Sheet",0);

				sheet.addCell(new Label(0,0,query.getConditionCodeName()));
				sheet.addCell(new Label(1,0,"产品标识码"));

				int colsum = 0;
				for(String bn:query.getBindingCodeNameList()){
					sheet.addCell(new Label((colsum++) + 2,0,bn));
				}

				int row = 1;
				for (int j = 0; j < resultCopy.size(); j++) {
					sheet.addCell(new Label(0,row,resultCopy.get(j).getQueryCode()));
					sheet.addCell(new Label(1,row,resultCopy.get(j).getIdCode()));
					switch(colsum){
						case 9:
							sheet.addCell(new Label(10,row,resultCopy.get(j).getCode_8()));
						case 8:
							sheet.addCell(new Label(9,row,resultCopy.get(j).getCode_7()));
						case 7:
							sheet.addCell(new Label(8,row,resultCopy.get(j).getCode_6()));
						case 6:
							sheet.addCell(new Label(7,row,resultCopy.get(j).getCode_5()));
						case 5:
							sheet.addCell(new Label(6,row,resultCopy.get(j).getCode_4()));
						case 4:
							sheet.addCell(new Label(5,row,resultCopy.get(j).getCode_3()));
						case 3:
							sheet.addCell(new Label(4,row,resultCopy.get(j).getCode_2()));
						case 2:
							sheet.addCell(new Label(3,row,resultCopy.get(j).getCode_1()));
						case 1:
							sheet.addCell(new Label(2,row,resultCopy.get(j).getCode_0()));
					}
					row++;
				}
				workbook.write();
				resultCopy.clear();
				filenameList.add(filename);
				workbook.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (RowsExceededException e) {
				e.printStackTrace();
			} catch (WriteException e) {
				e.printStackTrace();
			}
		}
		return filenameList;
	}
}
